<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="登录名" prop="username">
        <el-input
          v-model="queryParams.username"
          placeholder="请输入用户登录名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="真实姓名" prop="realname">
        <el-input
          v-model="queryParams.realname"
          placeholder="请输入真实姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="上级名称" prop="mobile">
        <el-input
          v-model="queryParams.pname"
          placeholder="请输入上级真实姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="性别" prop="gender">
        <el-select v-model="queryParams.gender" placeholder="请选择性别" @change="handleQuery">
          <el-option
            v-for="item in dict.type.sys_user_sex"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>


      <el-form-item label="手机号码" prop="mobile">
        <el-input
          v-model="queryParams.mobile"
          placeholder="请输入手机号码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="会员等级" prop="level">
        <el-select v-model="queryParams.level" placeholder="请选择会员等级" @change="handleQuery">
          <el-option
            v-for="item in dict.type.user_level"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="是否分佣" prop="isCommission">
        <el-select v-model="queryParams.isCommission" placeholder="请选择" @change="handleQuery">
          <el-option
            v-for="item in dict.type.is_commission"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" @change="handleQuery">
          <el-option
            v-for="item in dict.type.good_status"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="注册时间">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>



      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['video:DUser:add']"-->
<!--        >新增</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['video:DUser:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['video:DUser:remove']"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['video:DUser:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="DUserList">
      <el-table-column label="会员ID" align="center" width="180px" prop="id" />
      <el-table-column label="用户登录名" align="center" prop="username" />
      <el-table-column label="真实姓名" align="center" prop="realname" />
      <el-table-column label="手机号码" align="center" width="150px" prop="mobile" />
      <el-table-column label="电子邮箱" align="center" width="180px" prop="email" />
<!--      <el-table-column label="会员等级" align="center" prop="level">-->
<!--        <template slot-scope="scope">-->
<!--          <span v-if="scope.row.level == '1'">一般用户</span>-->
<!--          <span v-if="scope.row.level == '2'">vip用户</span>-->
<!--          <span v-if="scope.row.level == '3'">svip用户</span>-->
<!--          <span v-if="scope.row.level == '4'">代理</span>-->
<!--          <span v-if="scope.row.level == '5'">合伙人</span>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column label="会员等级" align="center" prop="level">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.user_level" :value="scope.row.level"/>
        </template>
      </el-table-column>
      <el-table-column label="到期时间" align="center" prop="dayNum" />
      <el-table-column label="上级名称" align="center" prop="pname" />
      <el-table-column label="钱包余额" align="center" prop="balance" />
      <el-table-column label="赠送金额" align="center" prop="giveBalance" />
      <el-table-column label="返佣总额" align="center" prop="totalAmount" />
      <el-table-column label="返佣余额" align="center" prop="commissionBalance" />
      <el-table-column label="邀请码" align="center" prop="inviteCode" />
      <el-table-column label="是否分佣" align="center" prop="status">
        <template slot-scope="scope">
          <span v-if="scope.row.isCommission == 'Y'">是</span>
          <span v-if="scope.row.isCommission == 'N'">否</span>
        </template>
      </el-table-column>

      <el-table-column label="用户状态" align="center" prop="status">
        <template slot-scope="scope">
          <span v-if="scope.row.status == '1'">启用</span>
          <span v-if="scope.row.status == '2'">禁用</span>
        </template>
      </el-table-column>
      <el-table-column label="注册时间" align="center" width="180px" prop="createTime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['video:DUser:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleCz(scope.row)"
            v-hasPermi="['video:DEchargeRecord:add']"
          >充值</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleResetPwd(scope.row)"
            v-hasPermi="['video:DUser:remove']"
          >重置密码</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改客户对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="用户登录名" prop="username">
          <el-input v-model="form.username" placeholder="请输入用户登录名" disabled />
        </el-form-item>
        <el-form-item label="真实姓名" prop="realname">
          <el-input v-model="form.realname" placeholder="请输入真实姓名" disabled />
        </el-form-item>
        <el-form-item label="性别" prop="gender">
<!--          <el-input v-model="form.gender" placeholder="请输入性别 0男 1女 2未知" />-->
                      <el-radio-group v-model="form.gender" disabled>
                        <el-radio
                          v-for="dict in dict.type.sys_user_sex"
                          :key="dict.value"
                          :label="dict.value"
                        >{{dict.label}}</el-radio>
                      </el-radio-group>
        </el-form-item>
<!--        <el-form-item label="头像" prop="headimg">-->
<!--          <el-input v-model="form.headimg" placeholder="请输入头像" />-->
<!--        </el-form-item>-->
        <el-form-item label="手机号码" prop="mobile">
          <el-input v-model="form.mobile" placeholder="请输入手机号码"  />
        </el-form-item>
        <el-form-item label="电子邮箱" prop="email">
          <el-input v-model="form.email" placeholder="请输入电子邮箱"  />
        </el-form-item>

        <el-form-item label="会员等级" prop="level">
          <el-select v-model="form.level" placeholder="请选择会员等级" @keyup.enter.native="handleQuery">
            <el-option
              v-for="item in dict.type.user_level"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
          </el-form-item>

          <el-form-item label="有效天数" prop="dayNum">
          <el-input v-model="form.dayNum" placeholder="请输入有效天数(天)" />
        </el-form-item>

        <el-form-item label="父id" prop="pid">
          <el-input v-model="form.pid" placeholder="请输入父id" />
        </el-form-item>

        <el-form-item label="是否分佣" prop="isCommission">
          <el-select v-model="form.isCommission" placeholder="请选择" >
            <el-option
              v-for="item in dict.type.is_commission"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择状态" >
            <el-option
              v-for="item in dict.type.good_status"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>


<!--        <el-form-item label="钱包余额" prop="balance">-->
<!--          <el-input v-model="form.balance" placeholder="请输入钱包余额" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="返佣总额" prop="totalAmount">-->
<!--          <el-input v-model="form.totalAmount" placeholder="请输入返佣总额" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="返佣余额" prop="commissionBalance">-->
<!--          <el-input v-model="form.commissionBalance" placeholder="请输入返佣余额" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="邀请码" prop="inviteCode">-->
<!--          <el-input v-model="form.inviteCode" placeholder="请输入邀请码" />-->
<!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 充值弹出框   -->
    <el-dialog :title="title2" :visible.sync="open2" width="500px" append-to-body>
      <el-form ref="form2" :model="form" :rules="rules2" label-width="100px">
        <el-input type="hidden" v-model="form2.uid"  />
        <el-form-item label="支付渠道" prop="type">
          <el-select v-model="form2.type" placeholder="请选择支付渠道">
            <el-option
              v-for="item in dict.type.pay_type"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="支付账号" prop="accountNumber">
          <el-input v-model="form2.accountNumber" placeholder="请输入支付账号" />
        </el-form-item>
        <el-form-item label="充值金额" prop="amount">
          <el-input v-model="form2.amount" placeholder="请输入充值金额" />
        </el-form-item>
        <el-form-item label="赠送金额" prop="giveAmount">
          <el-input v-model="form2.giveAmount" placeholder="请输入赠送金额" />
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFormCz">确 定</el-button>
        <el-button @click="cancelCz">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listDUser,
  getDUser,
  delDUser,
  addDUser,
  updateDUser,
  resetUserPwd
} from "@/api/video/DUser";
import {addDEchargeRecord} from "@/api/video/DEchargeRecord";


export default {
  name: "DUser",
  dicts: ['user_level','sys_user_sex','good_status','pay_type','is_commission'],

  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 客户表格数据
      DUserList: [],
      // 弹出层标题
      title: "",
      title2: "",
      // 是否显示弹出层
      open: false,
      open2: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        username: null,
        realname: null,
        pname: null,
        gender: null,
        mobile: null,
        level: null,
        isCommission: null,
        status: null,
        beginTime: null,
        endTime: null,
        dayNum: null
      },
      // 表单参数
      form: {},
      form2: {},
      // 表单校验
      rules: {
      },
      rules2: {
      },
      // 日期范围
      dateRange: [],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询客户列表 */
    getList() {
      this.loading = true;
      if(this.dateRange.length>0){
        this.queryParams.beginTime=this.dateRange[0];
        this.queryParams.endTime=this.dateRange[1];
      }else{
        this.queryParams.beginTime=null;
        this.queryParams.endTime=null;
      }
      listDUser(this.queryParams).then(response => {
        this.DUserList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    cancelCz(){
      this.open2 = false;
      this.reset2();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        username: null,
        password: null,
        realname: null,
        gender: null,
        mobile: null,
        level: null,
        pname: null,
        isCommission: null,
        status: null
      };
      this.resetForm("form");
    },
    // 表单重置
    reset2() {
      this.form2 = {
        uid: null,
        type: null,
        accountNumber: null,
        amount: null,
        giveAmount:null
      };
      this.resetForm("form2");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange= [];
      this.queryParams.pname=null;
      this.queryParams.beginTime=null;
      this.queryParams.endTime=null;
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    // handleSelectionChange(selection) {
    //   this.ids = selection.map(item => item.id)
    //   this.single = selection.length!==1
    //   this.multiple = !selection.length
    // },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加客户";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getDUser(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改客户";
      });
    },
    /**充值 */
    handleCz(row) {
      this.reset2();
      const id = row.id || this.ids
      this.form2.uid=id;
      this.open2 = true;
      this.title2 = "充值";
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateDUser(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDUser(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    //充值提交按钮
    submitFormCz() {
      this.$refs["form2"].validate(valid => {
        if (valid) {
          addDEchargeRecord(this.form2).then(response => {
            this.$modal.msgSuccess("充值成功");
            this.open2 = false;
            this.getList();
          });
        }
      });
    },

    /** 重置密码按钮操作 */
    handleResetPwd(row) {
      this.$prompt('请输入"' + row.realname + '"的新密码', "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        closeOnClickModal: false,
        inputPattern: /^.{5,20}$/,
        inputErrorMessage: "用户密码长度必须介于 5 和 20 之间"
      }).then(({ value }) => {
        resetUserPwd(row.id, value).then(response => {
          this.$modal.msgSuccess("修改成功，新密码是：" + value);
          this.getList();
        });
      }).catch(() => {});
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除客户编号为"' + ids + '"的数据项？').then(function() {
        return delDUser(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('video/DUser/export', {
        ...this.queryParams
      }, `DUser_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
